package com.dsg.kf.common.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * Number格式化工具类
 * 
 * @author pacy.pan
 * @date 2017年2月18日
 *
 */
public class NumberUtil {
	
	private static final String strNumReg="[^0-9]";
	
	private static final String bafen0="%0";
	
	private static final String d="d";
	
	private static Long tmpID = 0l;

	public static byte toByte(Number number) {
		return number.byteValue();
	}

	public static double toDouble(Number number) {
		return number.doubleValue();
	}

	public static float toFloat(Number number) {
		return number.floatValue();
	}

	public static int toInt(Number number) {
		return number.intValue();
	}

	public static long toLong(Number number) {
		return number.longValue();
	}

	public static short toShort(Number number) {
		return number.shortValue();
	}

	public static BigDecimal toBigDecimal(Number number) {
		if (number instanceof BigDecimal) {
			return (BigDecimal) number;
		} else {
			return new BigDecimal(number.doubleValue());
		}
	}
	
	/**
     * 随机生成UUID 32位字符串
     * 
     * @return
     */
    public static String getUUID() {
        return (UUID.randomUUID().toString()).replace("-", "");
    }
	/**
	 * 生成随机N位数字符串
	 * 短信验证码
	 * @return
	 */
	public static String createRandom(int n){
		StringBuilder str = new StringBuilder();
		for(int i = 0; i < n; i++){
			str.append((int)(Math.random()*10));
		}
		return str.toString();
	}
	
	/**
	 * 从字符串里提取出数字
	 */
	public static String regStrNum(String str) {
		Pattern p = Pattern.compile(strNumReg);   
		Matcher m = p.matcher(str);   
		return m.replaceAll(StringUtils.EMPTY).trim();
	}
	
	/**
	 * 生成唯一ID
	 * @return
	 */
	public static Long getId(){
		Long ltime = Long.valueOf(new SimpleDateFormat(DateUtil.yyMMddHHmmssSSS)
			      .format(new Date()).toString())*10000;
		if (tmpID < ltime) {
		     tmpID = ltime;
	    } else {
	        tmpID = tmpID + 1;
	        ltime = tmpID;
	    }
	     return ltime;
	}
	/**
	 * 获得固定长度的数字字符串，不足前面补齐0
	 * @param code
	 * @param num
	 * @return
	 */
	public static String autoGenericCode(long code, int num) {
        String result = StringUtils.EMPTY;
        result = String.format(bafen0 + num + d, code);
        return result;
    }
	
	public static void main(String args[]){
		for(int s=1;s<=1000000;s++){
			System.out.println(autoGenericCode(s,10));
		}
	}
}